package com.erp.controller;


import com.alibaba.excel.EasyExcel;
import com.erp.entity.Warehouse;
import com.erp.entity.result.Result;
import com.erp.entity.result.ResultStatus;
import com.erp.entity.vo.InputVO;
import com.erp.service.WarehouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pyj
 * @since 2020-11-07
 */
@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    @GetMapping("/exportAllWarehouse")
    public void exportAllWarehouse(HttpServletResponse response) throws IOException {
        List<Warehouse> list = warehouseService.getAll();
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("Warehouse", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), InputVO.class).sheet("表格").doWrite(list);
    }
    
    @GetMapping("/getWarehouseByPage")
    public Result getWarehouseByPage(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "8") int pageSize) {

        PageHelper.startPage(page,pageSize);
        List<Warehouse> list = warehouseService.getAll();
        PageInfo<Warehouse> pageInfo = new PageInfo<>(list);
        return new Result(ResultStatus.SUCCESS, pageInfo);
    }
    
    @GetMapping("/getAllWarehouse")
    public Result getAllWarehouse() {
        return new Result(ResultStatus.SUCCESS, warehouseService.getAll());
    }
    
    @PostMapping("/add")
    public Result insert(@RequestBody Warehouse warehouse) {

        if (warehouseService.add(warehouse) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }

    @GetMapping("/delete")
    public Result delete(@PathParam("id") Integer id) {
        System.out.println(id);
        if (warehouseService.delete(id) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Warehouse warehouse) {

        if (warehouseService.update(warehouse) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }
}

